﻿      $(document).ready(function() {
          var categoryIds = $("#categoryIds").val().split('|');

          for (var intLoop = 0; intLoop < categoryIds.length - 1; intLoop++) {
              var newId = "#spancategory" + categoryIds[intLoop];
              $("#spancategory" + categoryIds[intLoop]).addClass("tagSelected");
          }
          $("#lnkChange").click(function() {
              $("#divUpload").slideToggle("fast");
          });

          $("#btnCancel").click(function() {
              $("#divUpload").slideToggle("fast");
              document.getElementById('userFileUpload').value = "";
              //$("#userFileUpload").val('');
          });

          $("#btn-upload").click(function() {
              if ($("#isFileValid").val() != "false") {
                  DisplayPhoto();
              }
              else {
                  return false;
              }
          });

          function DisplayPhoto() {

              $.ajaxFileUpload({
                  url: '/Account/PreviewImage',
                  secureuri: false,
                  fileElementId: 'userFileUpload',
                  dataType: '',
                  beforeSend: function() { },
                  complete: function() { },
                  success: function(data, status) { $("#divUpload").slideToggle("fast"); $("#imgThumb").attr("src", "/Account/Temp/?id=" + Date()); },
                  error: function(data, status, e) { }
              });
              return false;
          }

          $("#cancelForm").click(function() {
              $.post("/Account/CancelRegister", {}, function(data) {
              });
              window.location = "/Home";
          });


          $("#saveForm").click(function() {
              var categoryIds = "";
              $("#interests").val("");
              $('span.tagSelected').each(function() {
                  var data = $(this).metadata();
                  categoryIds = categoryIds + data.CategoryId + ",";
                  $("#interests").val($("#interests").val() + $(this).text() + ",");
              });
              $("#interests").val($("#interests").val() + "?" + categoryIds);
          });
      });

      function validFileName(str) {
          str = str.replace(/^\s|\s$/g, ""); //trims string
          return /^[a-z$_@%#!&~()`!\s\-][\w$_@%#!&~()`!\s\-]+\.(bmp|jpeg|jpg|gif|png)$/i.test(str);
      }

      function checkExtension(ctrl) {
          var file = ctrl.value;
          var type = "";
          var browser = navigator.appName;
          var fileLength;
          var ext = file.slice(file.lastIndexOf(".")).toLowerCase();
          
          if (browser == "Microsoft Internet Explorer") {
              type = file.slice(file.indexOf("\\") + 1);
              fileLength = file.slice(file.lastIndexOf("\\")).toLowerCase().length - ext.length;
              var fileName = file.slice(file.lastIndexOf("\\")).toLowerCase();
              fileName = fileName.substring(1, fileName.length)
          }
          else if (browser == "Netscape") {
            fileName = file;
            fileLength = fileName.length - ext.length;
          }
          if (fileLength <= 1000) {
              if (validFileName(fileName)) {
                  $("#isFileValid").val("true");
                  $("#divFileUpload").html("");
              }
              else {
                  $("#isFileValid").val("false");
                  $("#divFileUpload").html("").append("<div class='requiredfield'>Please enter valid file name</div>");
              }
          }
          else {
              $("#isFileValid").val("false");
              $("#divFileUpload").html("").append("<div class='requiredfield'>Please check file name length</div>");
          }
      }
        
